﻿using DLL;
using gregn6Lib;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.SBAR
{
    public partial class PrintSBAR : Form
    {
        #region 变量声明
        private GridppReport Report = new GridppReport();    //引用 gregn6Lib
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        PatientInfo PatientInfo;
        DateTime Start;
        #endregion

        #region 构造函数
        public PrintSBAR(PatientInfo patientInfo, DateTime StartDate)
        {
            InitializeComponent();
            Start = StartDate;
            PatientInfo = patientInfo;
            //载入报表模板数据
            Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\护士晨会SBAR交班记录单.grf");
            //设置与数据源的连接串，因为在设计时指定的数据库路径是绝对路径。
            Report.ConnectionString = Utility.GetDatabaseConnectionString();
            //连接报表取数事件
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            //设定查询显示器关联的报表
            axGRPrintViewer1.Report = Report;
        }
        #endregion

        #region 连接报表取数事件
        private void ReportFetchRecord()
        {
            DataTable dt = BindInfo(Start);
            DataTable dtt = new DataTable();
            CreateDateTableHead(dtt);
            string sqlStr = string.Format("SELECT NursingFormData FROM NursingPublicRelation(NOLOCK) WHERE FormId='{0}'AND AreaId='{1}' AND AssessmentDate='{2}' ",
           (int)FormType.HandOverDuty, PatientInfo.NURSE_CELL_CODE, Start);
            DataTable table = SqlHelper.ExecuteDataset(sqlStr, System.Data.CommandType.Text).Tables[0];
            var SNursingFormData1 = Convert.ToString(table.Rows[0]["NursingFormData"]);
            SBARNursingRelation nr = new SBARNursingRelation();
            nr = XmlUtil.XMLToObject<SBARNursingRelation>(SNursingFormData1, Encoding.UTF8);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dtt.Rows.Add(
                            dt.Rows[i]["PatientName"].ToString(),
                            dt.Rows[i]["BedNo"].ToString(),
                            dt.Rows[i]["SEX"].ToString(),
                            dt.Rows[i]["PATIENT_ID"].ToString(),
                            nr.BingRenZongshu,
                            nr.XinRuYuan,
                            nr.ChuYuan,
                            nr.ZhuanRu,
                            nr.ZhuanChu,
                            nr.SiWang,
                            nr.YiJiBingRen,
                            dt.Rows[i]["RiQi"].ToString(),
                            dt.Rows[i]["ShiJian"].ToString(),
                            dt.Rows[i]["BingRenLieBie1"].ToString().Equals("√") ? 1 : 0,
                            dt.Rows[i]["BingRenLieBie2"].ToString().Equals("√") ? 1 : 0,
                            dt.Rows[i]["BingRenLieBie3"].ToString().Equals("√") ? 1 : 0,
                            dt.Rows[i]["BingRenLieBie4"].ToString().Equals("√") ? 1 : 0,
                            dt.Rows[i]["HuliJibie1"].ToString().Equals("√") ? 1 : 0,
                            dt.Rows[i]["HuliJibie2"].ToString().Equals("√") ? 1 : 0,
                            dt.Rows[i]["HuliJibie3"].ToString().Equals("√") ? 1 : 0,
                            dt.Rows[i]["RuYuanZhenDuan"],
                            dt.Rows[i]["JiaoBanBingQingXianZhuang"],
                            dt.Rows[i]["JiaoBanBingQingBeiJing"],
                            dt.Rows[i]["JiaoBanBingQingPingGu"],
                            dt.Rows[i]["JiaoBanBingQingJianYi"],
                            dt.Rows[i]["JiaoBanRen"]
                );
            }
            //将 DataTable 的数据转储到 Grid++Report 的数据集中
            dtt.DefaultView.Sort = "RiQi ASC,ShiJian ASC";
            DataTable data = dtt.DefaultView.ToTable();
            Utility.FillRecordToReport(Report, data);

        }
        #endregion

        #region 创建列头
        private static void CreateDateTableHead(DataTable dt)
        {
            dt.Columns.Add("PatientName", typeof(string));             //患者姓名
            dt.Columns.Add("BEDNO", typeof(string));                     //床号 
            dt.Columns.Add("sex", typeof(string));                     //患者性别
            dt.Columns.Add("PATIENT_ID", typeof(string));               //患者住院号
            dt.Columns.Add("BingRenZongshu", typeof(string));                //病人总数
            dt.Columns.Add("XinRuYuan", typeof(string));            //新入院
            dt.Columns.Add("ChuYuan", typeof(string));             //出院
            dt.Columns.Add("ZhuanRu", typeof(string));             //转入
            dt.Columns.Add("ZhuanChu", typeof(string));         //转出
            dt.Columns.Add("SiWang", typeof(string));         //死亡
            dt.Columns.Add("YiJiBingRen", typeof(string));         //一级病
            dt.Columns.Add("riqi", typeof(string));                    //日期
            dt.Columns.Add("shijian", typeof(string));                 //时间
            dt.Columns.Add("BingRenLieBie1", typeof(string));
            dt.Columns.Add("BingRenLieBie2", typeof(string));
            dt.Columns.Add("BingRenLieBie3", typeof(string));
            dt.Columns.Add("BingRenLieBie4", typeof(string));
            dt.Columns.Add("HuliJibie1", typeof(string));
            dt.Columns.Add("HuliJibie2", typeof(string));
            dt.Columns.Add("HuliJibie3", typeof(string));
            dt.Columns.Add("RuYuanZhenDuan", typeof(string));
            dt.Columns.Add("JiaoBanBingQingXianZhuang", typeof(string));
            dt.Columns.Add("JiaoBanBingQingBeiJing", typeof(string));
            dt.Columns.Add("JiaoBanBingQingPingGu", typeof(string));
            dt.Columns.Add("JiaoBanBingQingJianYi", typeof(string));
            dt.Columns.Add("JiaoBanRen", typeof(string));
        }
        #endregion

        private void PrintSBAR_Load(object sender, EventArgs e)
        {
            axGRPrintViewer1.Start();
        }

        public DataTable BindInfo(DateTime dateE_StartTime)
        {
            string patientid = this.PatientInfo.PATIENT_ID;   //住院号
            int VisitId = this.PatientInfo.VISIT_ID;   //住院次数
            int FormId = (int)FormType.HandOverDuty;    //表单ID
            string AreaId = this.PatientInfo.NURSE_CELL_CODE; //病区ID
            DataTable dt = new DataTable();
            dt.Columns.Add("RiQi", typeof(string));
            dt.Columns.Add("ShiJian", typeof(string));
            dt.Columns.Add("PatientName", typeof(string));
            dt.Columns.Add("BedNo", typeof(string));
            dt.Columns.Add("SEX", typeof(string));
            dt.Columns.Add("PATIENT_ID", typeof(string));
            dt.Columns.Add("BingRenLieBie1", typeof(string));
            dt.Columns.Add("BingRenLieBie2", typeof(string));
            dt.Columns.Add("BingRenLieBie3", typeof(string));
            dt.Columns.Add("BingRenLieBie4", typeof(string));
            dt.Columns.Add("HuliJibie1", typeof(string));
            dt.Columns.Add("HuliJibie2", typeof(string));
            dt.Columns.Add("HuliJibie3", typeof(string));
            dt.Columns.Add("RuYuanZhenDuan", typeof(string));
            dt.Columns.Add("JiaoBanBingQingXianZhuang", typeof(string));
            dt.Columns.Add("JiaoBanBingQingBeiJing", typeof(string));
            dt.Columns.Add("JiaoBanBingQingPingGu", typeof(string));
            dt.Columns.Add("JiaoBanBingQingJianYi", typeof(string));
            dt.Columns.Add("JiaoBanRen", typeof(string));
            dt.Columns.Add("Id", typeof(string));
            var ret = formService.GetNursingFormRecordByDate(patientid, VisitId, FormId, Convert.ToDateTime(dateE_StartTime), Convert.ToDateTime(dateE_StartTime));
            if (ret.Code == 1)
            {
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderByDescending(p => p.AssessmentDate).ToList();
                if (!string.IsNullOrEmpty(AreaId))
                    list = list.Where(p => p.AreaId == AreaId).ToList();
                foreach (NursingFormRecord record in list)
                {
                    var NursingFormData = record.NursingFormData;
                    SBARHandOverDuty sbar = new SBARHandOverDuty();
                    sbar = XmlUtil.XMLToObject<SBARHandOverDuty>(NursingFormData, Encoding.UTF8);
                    DataRow newRow = dt.NewRow();
                    newRow["RiQi"] = Convert.ToDateTime(sbar.RiQi).ToString("yyyy-MM-dd");   //日期
                    newRow["ShiJian"] = sbar.ShiJian;   //时间
                    newRow["PatientName"] = record.PatientName;// PatientInfo.PATIENT_NAME;  //姓名
                    newRow["BedNo"] = record.BedNo;// PatientInfo.BED_NO; //床号
                    newRow["SEX"] = GetSex(record.PatientId);  //性别
                    newRow["PATIENT_ID"] = record.PatientId; //PatientInfo.PATIENT_ID; //病案号
                    //病人类别
                    string leibie = sbar.BingRenLieBie;
                    string[] LeiBieArray;
                    if (!string.IsNullOrEmpty(leibie))
                    {
                        LeiBieArray = leibie.Split('#');
                        if (Convert.ToInt32(LeiBieArray[0]) == 1)
                        {
                            newRow["BingRenLieBie1"] = "√";
                        }
                        if (Convert.ToInt32(LeiBieArray[1]) == 1)
                        {
                            newRow["BingRenLieBie2"] = "√";
                        }
                        if (Convert.ToInt32(LeiBieArray[2]) == 1)
                        {
                            newRow["BingRenLieBie3"] = "√";
                        }
                        if (Convert.ToInt32(LeiBieArray[3]) == 1)
                        {
                            newRow["BingRenLieBie4"] = "√";
                        }
                    }
                    //护理级别
                    string huliijibie = sbar.HuLiJiBie;
                    string[] JiBieArray;
                    if (!string.IsNullOrEmpty(huliijibie))
                    {
                        JiBieArray = huliijibie.Split('#');
                        if (Convert.ToInt32(JiBieArray[0]) == 1)
                        {
                            newRow["HuliJibie1"] = "√";
                        }
                        if (Convert.ToInt32(JiBieArray[1]) == 1)
                        {
                            newRow["HuliJibie2"] = "√";
                        }
                        if (Convert.ToInt32(JiBieArray[2]) == 1)
                        {
                            newRow["HuliJibie3"] = "√";
                        }
                    }
                    newRow["RuYuanZhenDuan"] = sbar.RuYuanZhenDuan; //入院诊断
                    newRow["JiaoBanBingQingXianZhuang"] = sbar.JiaoBanBingQingXianZhuang;    //现状
                    newRow["JiaoBanBingQingBeiJing"] = sbar.JiaoBanBingQingBeiJing;  //背景
                    newRow["JiaoBanBingQingPingGu"] = sbar.JiaoBanBingQingPingGu; //评估
                    newRow["JiaoBanBingQingJianYi"] = sbar.JiaoBanBingQingJianYi; //建议
                    newRow["JiaoBanRen"] = sbar.JiaoBanRen;    //签名
                    //ID隐藏，双击触发事件
                    newRow["Id"] = record.Id;
                    dt.Rows.Add(newRow);
                }
            }
            dt.DefaultView.Sort = "RiQi DESC,ShiJian DESC";
            DataTable data = dt.DefaultView.ToTable();
            return data;
        }

        //获取患者性别
        private object GetSex(string patientId)
        {
            string sqlStr = string.Format("SELECT TOP 1 SEX FROM V_YDHL_PATIENT(NOLOCK) WHERE PATIENT_ID='{0}' ", patientId);
            return SqlHelper.ExecuteScalar(sqlStr);
        }
    }
}
